<#if packageStyle == "service">
package ${sourceBasePackage}.${entityPackage}.controller;

import ${sourceBasePackage}.${entityPackage}.entity.${className};
import ${sourceBasePackage}.${entityPackage}.service.${className}Service;
<#else>
package ${sourceBasePackage}.controller.${entityPackage};

import ${sourceBasePackage}.entity.${entityPackage}.${className};
import ${sourceBasePackage}.service.${entityPackage}.${className}Service;
</#if>
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.model.Response;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;

/**
 * ${description}controller
 * @author ${author}
 * @date ${createDate}
 */
@Controller
@RequestMapping("/${className?uncap_first}")
public class ${className}Controller extends BaseController {

	private Logger logger = Logger.getLogger(${className}Controller.class);

	@Resource
	private ${className}Service ${className?uncap_first}Service;
	
	/** 初始化查询页面 */
	@RequestMapping("/init")
	public String init(){
		
		return "${webPackage}/${className?uncap_first}Search";
	}
	
	/** 分页查询 */
	@RequestMapping("/list")
	public String list(${className} ${className?uncap_first},Page<${className}> page,HttpServletRequest request){
		try {
			request.setAttribute("page", ${className?uncap_first}Service.findByPage(${className?uncap_first}, page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "${webPackage}/${className?uncap_first}List";
	}

	/** 新增修改页面初始化 */
	@RequestMapping("/${className?uncap_first}")
	public String ${className?uncap_first}(${pkType} ${pkField},HttpServletRequest request){
		try {
			if(ValidateUtils.isNotEmpty(${pkField})){
				${className} ${className?uncap_first} = ${className?uncap_first}Service.getById(${pkField});
				request.setAttribute("${className?uncap_first}", ${className?uncap_first});
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "${webPackage}/${className?uncap_first}";
	}
	
	/** 保存  */
	@RequestMapping("/save")
	@ResponseBody
	public Response save(${className} ${className?uncap_first}){
		Response json = new Response();
		try {
			// 修改
			if(ValidateUtils.isNotEmpty(${className?uncap_first}.get${pkField?cap_first}())){
				${className?uncap_first}Service.update(${className?uncap_first});
			}
			// 新增
			else{
				${className?uncap_first}Service.add(${className?uncap_first});
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("保存失败");
		}
		return json;
	}
	
	/** 删除  */
	@RequestMapping("/delete")
	@ResponseBody
	public Response delete(${pkType} ${pkField}){
		Response json = new Response();
		try {
			${className?uncap_first}Service.delete(${pkField});
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("删除失败");
		}
		return json;
	}
}
